<?php
// Run from command prompt > php -q chatbot.demo.php
require_once("websocket/websocket.server.php");

class PongServer extends WebSocketServer {
	protected $debug = true;
	public function __construct($address, $port){
		parent::__construct($address, $port);
	
		$this->addResourceHandler("pong", new GameHandler($this->users));
	}
	protected function onConnect(WebSocketUser $user){
		$this->say("[DEMO] {$user->getId()} connected");
	}
	
	public function onMessage($user, IWebSocketMessage $msg){
		$this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'");
	}
	
	protected function onDisconnect(WebSocketUser $user){
		$this->say("[DEMO] {$user->getId()} disconnected");
	}
	public function getAdminKey(){
		return "superadminkey";
	}
	protected function onAdminTest(IWebSocketUser $user){
		$this->say("[DEMO] Admin TEST received!");
	}
}
/*
reichet daten einfach durch

*/
class GameHandler extends WebSocketResourceHandler {
	var $userArray;

	public function __construct($userArray){
		$this->users = new SplObjectStorage();
		$this->userArray = $userArray;
	}
	public function onMessage(IWebSocketUser $user, IWebSocketMessage $msg){
		$decode = json_decode($msg->getData());
		$com = $decode[0];
		$target = $decode[1];
		$data = $decode[2];
		switch($com){
			case 'pickup':
				$this->server->send($this->getUser($target), $msg->getData());
				break;
			case 'ball':
				$this->server->send($this->getUser($target), $msg->getData());
				break;
			case 'goal':
				$this->server->send($this->getUser($target), $msg->getData());
				break;
			case 'player':
				$this->server->send($this->getUser($target), $msg->getData());
				break;

			case 'setName':
				$inUse = false;
				foreach($this->users as $client){
					if($client->name == $decode[1]){
						$inUse = true;
						break;
					}
				}
				$return = array('setName');
				if(!$inUse){
					$user->name = $decode[1];
					$return[] = 'accept';
					$return[] = $decode[1];
				}else{
					$return[] = 'error';
					$return[] = 'Name allready in use!!';
				}
				$this->server->send($user, json_encode($return));
				break;
			case 'ask4match':
				//random gegner
				$this->server->send($this->getUser($target), $msg->getData());
				break;
			case 'matchok':
				print_r($decode);
				$this->server->send($this->getUser($target), $msg->getData());
				break;
			case 'matchdeny':
				$this->server->send($this->getUser($target), $msg->getData());
				break;
			case 'getplayers'://return liste mit uid's
				$users = array();
				foreach($this->users as $key=>$value){
					if($user->getId() != $value->getId()){
						$users[] = $value->name;
					}
				}
				$return = array('getplayers', $users);

				$this->server->send($user, json_encode($return));
				break;
			case '':
				//$this->server->send(WebSocketUser $client, $str)
				break;




		}

		//print_r($obj).' ### ';
		//$this->send($this->client, $str);
	}
	public function getUser($name){
		foreach($this->users as $user){
			if($user->name == $name){
				return $user;
			}
		}
	}
	public function onAdminMessage(IWebSocketUser $user, stdClass $msg){
		$obj = json_decode($msg->getData());
		echo $msg->getData().' ### ';
	}

}


// $server = new PongServer('85.25.193.8',8088);
$server = new PongServer('localhost',8088);
$server->run();